<?php
namespace app\admin\controller;

use app\admin\inter_logic\CarLogic;

use app\admin\model\OrderLogModel;
use app\api\logic\AuthLogic;
use app\api\logic\OrderLogic;
use app\api\logic\TripLogic;
use app\api\logic\UserLogic;
use app\api\model\OrderModel;
use my\Security;
use think\Db;
use think\Env;

class OrderLocalLog extends Base
{
    protected $map = array('order_status'=>array('egt',40));//城内的订单

    public function _initialize()
    {
        parent::_initialize();
        $base_condition = $this->base_condition;
        $base_condition&&$this->map['start_code'] = $base_condition['region_code'];
    }

    public function index(){
        $config['url'] = url('order_local_log/table');
        $config['order_status'] = array(40=>"已下车",45=>"已付款",50=>"已评价");
        $config['pay_type'] = pay_type();
        $config['trajectory'] = Env::get('coordinate'.ENV.'_orderTrajectory');
        $this->assign($config);
        return view();
	}

	public function table()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $order_logic = new OrderModel();
        $res = $order_logic->getOrderList('*',$this->map, $page, $limit);
        $count = Db::table('xm_order_common')->where($this->map)->count();
        $res = decode_array_2($res);
        $auth_logic = new AuthLogic();
        foreach ($res as $key => $val){
            $where['driver_id'] = $val['driver_id'];
            $where['record_status'] = 2;
            $res[$key]['car_plate'] = Security::decrypt($auth_logic->getCarInfo($where,'car_plate')['car_plate']);
        }
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

	//搜索
    public function search()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $type = input('get.type');
        $start = strtotime(input('get.start'));
        $end = strtotime(input('get.end'));
        $order_status = input('get.order_status');
        $car_plate = trim(input('get.car_plate'),' ');
        $phone = trim(input('get.user_phone'),' ');
        $map = $this->map;
        switch ($type){
            //order_time
            case 1:
                input('get.start')&&$map['order_time'] = array('egt',$start);
                input('get.end')&&$map['order_time'] = array('elt',$end);
                input('get.start')&&input('get.end')&&$map['order_time'] = array('between',array($start,$end));
                break;
            //start_time
            case 2:
                input('get.start')&&$map['on_car_time'] = array('egt',$start);
                input('get.end')&&$map['on_car_time'] = array('elt',$end);
                input('get.start')&&input('get.end')&&$map['on_car_time'] = array('between',array($start,$end));
                break;
            case 3:
                input('get.start')&&$map['off_car_time'] = array('egt',$start);
                input('get.end')&&$map['off_car_time'] = array('elt',$end);
                input('get.start')&&input('get.end')&&$map['off_car_time'] = array('between',array($start,$end));
                break;
            default:
                break;
        }
        $order_status&&$map['order_status']=$order_status;
        if($phone){
            $user_logic = new UserLogic();
            $map['uid'] = $user_logic->getUserByPhone($phone,'uid')['uid'];
        }
        if($car_plate){
            $where['car_plate'] = array('like',"%$car_plate%");
            $where['record_status'] = 2;
            $car_ids = DB::table('xm_car_info')->where($where)->column('car_id');
            $map['car_id'] = array('in',$car_ids);
        }
        $order_logic = new OrderModel();
        $res = $order_logic->getOrderList('*',$map, $page, $limit);
        $count = Db::table('xm_order_common')->where($map)->count();
        $res = decode_array_2($res);
        $auth_logic = new AuthLogic();
        foreach ($res as $key => $val){
            $where['driver_id'] = $val['driver_id'];
            $where['record_status'] = 2;
            $res[$key]['car_plate'] = Security::decrypt($auth_logic->getCarInfo($where,'car_plate')['car_plate']);
        }
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

    //行程轨迹---转为xmcx服务器使用20180925
    public function local()
    {
        $common_id = input('get.common_id');
        if($common_id){
            $order_logic = new TripLogic();
            $order_info = $order_logic->getTripById($common_id,'local_table');
            if($order_info['local_table']){//longitude,latitude
                $local_info = Db::name($order_info['local_table'])->where('order_id',$common_id)->field('longitude,latitude')->select();
                if($local_info){
                    $index = 0;
                    foreach ($local_info as $key => $val){
                        if($val['longitude']=='0.0' || $val['longitude']=='0.0'){
                            continue;
                        }
                        $data[$index][] = $val['longitude'];
                        $data[$index][] = $val['latitude'];
                        $index++;
                    }
                    $config['protocol'] = PROTOCOL;
                    $config['center'] = json_encode($data[0]);
                    $config['local'] = json_encode($data);
                    $this->assign($config);
                    return view();
                }
            }
        }
    }

}